package com.homework.action;

import java.sql.SQLException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import com.homework.bean.CourseBean;
import com.homework.bean.HomeworkBean;
import com.homework.bean.HomeworkListBean;
import com.homework.bean.PageInfoBean;
import com.homework.bean.StudentHomeworkListBean;
import com.homework.bean.UserBean;
import com.homework.service.HomeworkService;

public class HomeworkShowAction {

	private HomeworkService homeworkService;
	private int courseId;
	private int intPage;
	private int score;
	private int homeworkId;
	private String homeworkState;
	
	public HomeworkShowAction() {
		// TODO Auto-generated constructor stub
		homeworkService = new HomeworkService();
	}

	public int getIntPage() {
		return intPage;
	}

	public void setIntPage(int intPage) {
		this.intPage = intPage;
	}

	public int getCourseId() {
		return courseId;
	}

	public void setCourseId(int courseId) {
		this.courseId = courseId;
	}

	public int getScore() {
		return score;
	}

	public void setScore(int score) {
		this.score = score;
	}

	public int getHomeworkId() {
		return homeworkId;
	}

	public void setHomeworkId(int homeworkId) {
		this.homeworkId = homeworkId;
	}

	public String getHomeworkState() {
		return homeworkState;
	}

	public void setHomeworkState(String homeworkState) {
		this.homeworkState = homeworkState;
	}

	//作业列表_教师
	public String homeworkShow_teacher() throws SQLException {
		
		HttpServletRequest request = ServletActionContext.getRequest();
		
		// courseBean
		CourseBean courseBean = new CourseBean();
		courseBean.setCourseId(String.valueOf(this.getCourseId()));
		// pageInfoBean
		PageInfoBean pageInfoBean = new PageInfoBean();
		pageInfoBean.setIntPage(this.getIntPage());
		// courseId
		int courseIdTemp = this.getCourseId();

		// 处理和设置pageInfoBean,StudentHomeworkListBean
		StudentHomeworkListBean studentHomeworkListBean = new StudentHomeworkListBean();
		studentHomeworkListBean = homeworkService.homeworkShow_teacher(courseBean, pageInfoBean);
		request.setAttribute("studentHomeworkListBean", studentHomeworkListBean);
		request.setAttribute("pageInfoBean", pageInfoBean);
		request.setAttribute("courseId", courseIdTemp);
		if((this.getHomeworkState() != null) && this.getHomeworkState().equals("MOD")){
			request.setAttribute("homeworkState", "MOD");
		}	
		return "Success";
	}
	
	//作业列表_学生
	public String homeworkShow_student() throws SQLException {
		
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpSession session = request.getSession();
		
		// courseBean
		CourseBean courseBean = new CourseBean();
		courseBean.setCourseId(String.valueOf(this.getCourseId()));
		// pageInfoBean
		PageInfoBean pageInfoBean = new PageInfoBean();
		pageInfoBean.setIntPage(this.getIntPage());
		// userBean
		UserBean userBean = new UserBean();
		userBean.setUserId(String.valueOf(session.getAttribute("userId")));
		// courseId
		int courseIdTemp = this.getCourseId();

		// 处理和设置pageInfoBean,StudentHomeworkListBean
		StudentHomeworkListBean studentHomeworkListBean = new StudentHomeworkListBean();
		studentHomeworkListBean = homeworkService.homeworkShow_student(courseBean, pageInfoBean,userBean);
		request.setAttribute("studentHomeworkListBean", studentHomeworkListBean);
		request.setAttribute("pageInfoBean", pageInfoBean);
		request.setAttribute("courseId", courseIdTemp);

		return "Success";
	}
	
	// 作业列表_老师布置
	public String homeworkShow_arrange() throws SQLException{
		HttpServletRequest request = ServletActionContext.getRequest();
		
		// courseBean
		CourseBean courseBean = new CourseBean();
		courseBean.setCourseId(String.valueOf(this.getCourseId()));
		// pageInfoBean
		PageInfoBean pageInfoBean = new PageInfoBean();
		pageInfoBean.setIntPage(this.getIntPage());
		// courseId
		int courseIdTemp = this.getCourseId();
		
		HomeworkListBean homeworkListBean = new HomeworkListBean();
		homeworkListBean = homeworkService.homeworkShow_arrange(courseBean,pageInfoBean);
		request.setAttribute("homeworkListBean", homeworkListBean);
		request.setAttribute("pageInfoBean", pageInfoBean);
		request.setAttribute("courseId", courseIdTemp);
		
		return "Success";
	}
	
	// 修改成绩
	public String modifyScore(){
		HttpServletRequest request = ServletActionContext.getRequest();
		
		// homeworkBean
		HomeworkBean homeworkBean = new HomeworkBean();
		homeworkBean.setHomeworkId(String.valueOf(this.getHomeworkId()));
		homeworkBean.setScore(String.valueOf(this.getScore()));
		// courseId
		int courseIdTemp = this.getCourseId();
		
		homeworkService.modifyScore(homeworkBean);
		
		request.setAttribute("courseId", courseIdTemp);
		return "Success";
	}

}
